package org.smartboot.flow.core.attribute;


import org.smartboot.flow.core.DegradeCallback;
import org.smartboot.flow.core.component.Component;

import java.util.List;


/**
 * @author qinluo
 * @date 2022-11-12 18:46:30
 * @since 1.0.0
 */
@SuppressWarnings("unchecked")
public enum Attributes {

    /**
     * name
     */
    NAME("name", String.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setName((String) value);
        }
    },

    ROLLBACK("rollback", Boolean.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setRollback((Boolean) value);
        }
    },

    DEGRADABLE("degradable", Boolean.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setDegradable((Boolean) value);
        }
    },

    ASYNC("async", Boolean.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setAsync((Boolean) value);
        }
    },

    TIMEOUT("timeout", Long.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setTimeout(((Number) value).longValue());
        }
    },

    DEPENDS_ON("dependsOn", List.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            //noinspection unchecked
            component.setDependsOn((List<String>)value);
        }
    },

    /**
     * 控制组件开启或者关闭
     *
     * @since 1.0.0
     */
    ENABLED("enabled", Boolean.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setEnabled((Boolean) value);
        }
    },

    /**
     * 降级回调
     *
     * @since 1.0.2
     */
    DEGRADE_CALLBACK("degrade-callback", DegradeCallback.class) {
        @Override
        public <T, S> void apply(Component<T, S> component, Object value) {
            super.apply(component, value);
            component.setDegradeCallback((DegradeCallback<T, S>) value);
        }
    },

    ;

    private final String name;

    /**
     * Attribute accepted type.
     */
    protected final Class<?> accept;

    Attributes(String name, Class<?> accept) {
        this.name = name;
        this.accept = accept;
    }

    public String getName() {
        return name;
    }

    public boolean accept(Object value) {
        return accept.isInstance(value);
    }

    public <T, S> void apply(Component<T, S> component, Object value) {
        component.addAttribute(AttributeHolder.of(this, value));
    }

    public static Attributes byName(String name) {
        for (Attributes v : values()) {
            if (v.name.equals(name)) {
                return v;
            }
        }

        return null;
    }
}
